package cst.a722wr.zyfypt_722wr.model;

import java.util.List;

import  cst.a722wr.zyfypt_722wr.Bean.ArticleBean;
import  cst.a722wr.zyfypt_722wr.Bean.CollectResult;
import  cst.a722wr.zyfypt_722wr.Bean.ProjectBean;
import  cst.a722wr.zyfypt_722wr.Bean.TcaseBean;
import  cst.a722wr.zyfypt_722wr.Bean.TwareBean;
import  cst.a722wr.zyfypt_722wr.Bean.VideoBean;
import  cst.a722wr.zyfypt_722wr.iface.CollectListListener;
import  cst.a722wr.zyfypt_722wr.iface.CollectListService;
import  cst.a722wr.zyfypt_722wr.iface.CollectListiface;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CollectListModel<T>  implements CollectListiface{
    private Retrofit retrofit;
    private String BASEURL = "http://amicool.neusoft.edu.cn/";
    private CollectListService service;

    public CollectListModel()
    {
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        service = retrofit.create(CollectListService.class);
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final CollectListListener listener) {
        switch (mod) {
            case "article":
                Call<List<CollectResult<ArticleBean>>> call_carticle = service.getCArticleList(mod, page, sessionID);
                call_carticle.enqueue(new Callback<List<CollectResult<ArticleBean>>>() {
                    @Override
                    public void onResponse(Call<List<CollectResult<ArticleBean>>> call, Response<List<CollectResult<ArticleBean>>> response) {
                        if(response.isSuccessful() && response!=null) {
                            listener.onResponse(response.body());
                        } else{
                            listener.onFail("onresponse fail");
                        }
                    }
                    @Override
                    public void onFailure(Call<List<CollectResult<ArticleBean>>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;

            case "tware":
                Call<List<CollectResult<TwareBean>>> call_tware = service.getCTwareList(mod, page, sessionID);
                call_tware.enqueue(new Callback<List<CollectResult<TwareBean>>>() {
                    @Override
                    public void onResponse(Call<List<CollectResult<TwareBean>>> call, Response<List<CollectResult<TwareBean>>> response) {
                        if(response.isSuccessful() && response!=null) {
                            listener.onResponse(response.body());
                        } else{
                            listener.onFail("onresponse fail");
                        }
                    }
                    @Override
                    public void onFailure(Call<List<CollectResult<TwareBean>>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;

            case "video":
                Call<List<CollectResult<VideoBean>>> call_video = service.getCVideoList(mod, page, sessionID);
                call_video.enqueue(new Callback<List<CollectResult<VideoBean>>>() {
                    @Override
                    public void onResponse(Call<List<CollectResult<VideoBean>>> call, Response<List<CollectResult<VideoBean>>> response) {
                        if(response.isSuccessful() && response!=null) {
                            listener.onResponse(response.body());
                        } else{
                            listener.onFail("onresponse fail");
                        }
                    }
                    @Override
                    public void onFailure(Call<List<CollectResult<VideoBean>>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;

            case "tcase":
                Call<List<CollectResult<TcaseBean>>> call_tcase = service.getCTcaseList(mod, page, sessionID);
                call_tcase.enqueue(new Callback<List<CollectResult<TcaseBean>>>() {
                    @Override
                    public void onResponse(Call<List<CollectResult<TcaseBean>>> call, Response<List<CollectResult<TcaseBean>>> response) {
                        if(response.isSuccessful() && response!=null) {
                            listener.onResponse(response.body());
                        } else{
                            listener.onFail("onresponse fail");
                        }
                    }
                    @Override
                    public void onFailure(Call<List<CollectResult<TcaseBean>>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                break;

            case "project":
                Call<List<CollectResult<ProjectBean>>> call_project = service.getCPojectList(mod, page, sessionID);
                call_project.enqueue(new Callback<List<CollectResult<ProjectBean>>>() {
                    @Override
                    public void onResponse(Call<List<CollectResult<ProjectBean>>> call, Response<List<CollectResult<ProjectBean>>> response) {
                        if(response.isSuccessful() && response!=null) {
                            listener.onResponse(response.body());
                        } else{
                            listener.onFail("onresponse fail");
                        }
                    }
                    @Override
                    public void onFailure(Call<List<CollectResult<ProjectBean>>> call, Throwable t) {
                        listener.onFail(t.toString());
                    }
                });
                    break;

            default:
                break;
            }
        }
    }
